package com.westa.smallshop.login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.westa.smallshop.MainActivity;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseFragment;
import com.westa.smallshop.register.RegisterActivity;
import com.westa.smallshop.shop.ShopActivity;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy123 on 2016/9/13.
 */
public class LoginFragment extends BaseFragment implements LoginContract.View {

    @BindView(R.id.et_user_account)
    AppCompatEditText etUserAccount;
    @BindView(R.id.et_user_password)
    AppCompatEditText etUserPassword;
    @BindView(R.id.iv_delete_phone)
    ImageView ivDeletePhone;
    @BindView(R.id.iv_delete_password)
    ImageView ivDeletePassword;

    @NonNull
    private LoginContract.Presenter mPresenter;

    @BindString(R.string.hint_title_login)
    String mTitle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void initView() {
        getActivity().setTitle(mTitle);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
    }

    public static LoginFragment newInstance() {

        return new LoginFragment();
    }

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_login;
    }

    @Override
    public void loginSuccessful() {

        ShopActivity.startActivity(getActivity());
        getActivity().finish();
    }

    @Override
    public void loginFailed(String errorMsg) {
        showSingleToast(errorMsg);
    }

    @Override
    public void showAccountEmptyError() {

        showSingleToast(R.string.empty_user_account_hint);
    }

    @Override
    public void showPasswordEmptyError() {
        showSingleToast(R.string.empty_user_password_hint);
    }

    @Override
    public void showLoginDialogProgress() {

        showDialogProgress(R.string.dlg_login_hint, new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mPresenter.cancelLogin();
            }
        });

    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void setPresenter(@NonNull LoginContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }


    @OnClick({R.id.iv_delete_phone, R.id.iv_delete_password, R.id.btn_login, R.id.tv_forget_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_delete_phone:
                etUserAccount.setText("");
                break;
            case R.id.iv_delete_password:
                etUserPassword.setText("");
                break;
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_forget_password:
//                ChangePasswordActivity.startActivity(getActivity());
                break;
        }
    }

    private void login() {

        mPresenter.doLogin(etUserAccount.getText().toString(), etUserPassword.getText().toString());
    }

    @OnTextChanged(R.id.et_user_account)
    public void onAccountTextChanged(CharSequence s, int start, int before, int count) {
        ivDeletePhone.setVisibility(s.length() > 0 ? View.VISIBLE : View.INVISIBLE);
    }

    @OnTextChanged(R.id.et_user_password)
    public void onPasswordTextChanged(CharSequence s, int start, int before, int count) {
        ivDeletePassword.setVisibility(s.length() > 0 ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.login_menu, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_register:
                RegisterActivity.startActivity(getActivity());
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
